// <copyright file="RockExplosionProjectile.cs" company="SpaceRock Team Members">
// Project: SpaceRock
// File: RockExplosionProjectile.cs (3,80 KB, 120 lines)
// Creation date: 2010-05-28 13:53
// Last modified: 2010-06-12 22:09
// </copyright>

#region File Description
//-----------------------------------------------------------------------------
// Projectile.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using directives
using Microsoft.Xna.Framework;
using SpaceRockTeam.SpaceRock.Data;
using System;
#endregion

namespace Particle3DSample
{
    /// <summary>
    /// Rock explosion projectile
    /// </summary>
    public class RockExplosionProjectile : IProjectile
    {
        /// <summary>
        /// 
        /// </summary>
        private const int numExplosionParticles = 10;

        /// <summary>
        /// 
        /// </summary>
        private const int numExplosionSmokeParticles = 20;

        /// <summary>
        /// 
        /// </summary>
        private const float projectileLifespan = 1.5f;

        /// <summary>
        /// Explosion particles
        /// </summary>
        private ParticleSystem explosionParticles;

        /// <summary>
        /// Explosion smoke particles
        /// </summary>
        private ParticleSystem explosionSmokeParticles;

        /// <summary>
        /// Position
        /// </summary>
        private Vector3 position;

        /// <summary>
        /// Velocity
        /// </summary>
        private Vector3 velocity;

        /// <summary>
        /// Age
        /// </summary>
        private float age;

        /// <summary>
        /// Should explode
        /// </summary>
        public bool ShouldExplode { get; set; } // ShouldExplode

        /// <summary>
        /// Explosion listener
        /// </summary>
        public IExplosionListener ExplosionListener { get; set; } // Explo

        /// <summary>
        /// Constructs a new projectile.
        /// </summary>
        public RockExplosionProjectile(ParticleSystem explosionParticles,
                          ParticleSystem explosionSmokeParticles,
                          Vector3 projectilePosition, Vector3 projectileVelocity,
            IExplosionListener explosionListener)
        {
            ExplosionListener = explosionListener;
            this.explosionParticles = explosionParticles;
            this.explosionSmokeParticles = explosionSmokeParticles;

            position = projectilePosition;
            velocity = projectileVelocity;
        } // RockExplosionProjectile


        /// <summary>
        /// Updates the projectile.
        /// </summary>
        public bool Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            position += velocity * elapsedTime;
            age += elapsedTime;

            // If enough time has passed, explode! Note how we pass our velocity
            // in to the AddParticle method: this lets the explosion be influenced
            // by the speed and direction of the projectile which created it.
            if (age > projectileLifespan || ShouldExplode)
            {
                // First the light
                for (int i = 0; i < numExplosionParticles; i++)
                    explosionParticles.AddParticle(position, velocity);

                for (int i = 0; i < numExplosionSmokeParticles; i++)
                    explosionSmokeParticles.AddParticle(position, velocity);

                // Than the sound :)
                if (ExplosionListener != null)
                    ExplosionListener.OnExplosion(this);
                return false;
            } // if
                
            return true;
        } // Update
    } // class RockExplosionProjectile
} // namespace Particle3DSample
